package com.wyy.mall.tiny.component;

import com.wyy.mall.tiny.dto.QueueEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * created by 伍猷煜 on 2021/5/25 14:27 星期二
 */
@Component
public class CancelOrderSender {
    private static Logger LOGGER = LoggerFactory.getLogger(CancelOrderSender.class);

    private final AmqpTemplate mAmqpTemplate;

    @Autowired
    public CancelOrderSender(AmqpTemplate mAmqpTemplate) {
        this.mAmqpTemplate = mAmqpTemplate;
    }

    public void sendMessage(Long orderId, final long delayTimes) {
        // 给延迟队列发送消息
        mAmqpTemplate.convertAndSend(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getExchange(),
                QueueEnum.QUEUE_TTL_ORDER_CANCEL.getRouteKey(),
                orderId,
                message -> {
                    // 给消息设置延迟毫秒值
                    message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
                    return message;
                });
        LOGGER.info("send delay message orderId:{}", orderId);
    }
}
